import {
  Body,
  Controller,
  Delete,
  Get,
  NotFoundException,
  Param,
  Post,
  Put,
  Request,
} from "@nestjs/common";
import { CreateCompanyDto } from "./dto/create-company.dto";
import { CompanyService } from "./company.service";
import { Roles } from "src/utils/decorators/roles.guard";
import { UpdateCompanyDto } from "./dto/update-company.dto";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";

@Controller("company")
@ApiTags("公司")
@ApiBearerAuth()
export class CompanyController {
  constructor(private readonly companyService: CompanyService) {}

  @Get("list")
  @Roles("admin")
  @ApiOperation({
    summary: "列出所有公司列表，只有管理员拥有该权限",
  })
  list() {
    return this.companyService.findAll();
  }

  @Put()
  @Roles("manager")
  @ApiOperation({
    summary: "修改当前登录用户的公司信息，经理权限",
  })
  updateAuth(@Body() updateCompanyDto: UpdateCompanyDto, @Request() req) {
    return this.companyService.update(req.user.company, updateCompanyDto);
  }

  @Get()
  @ApiOperation({
    summary: "获得当前登录用户的公司信息",
  })
  findAuth(@Request() req) {
    if (req.user.aud === "admin") throw new NotFoundException();
    return this.companyService.find(req.user.company);
  }

  @Post()
  @Roles("admin")
  @ApiOperation({
    summary: "新增公司，只有管理员拥有该权限",
  })
  create(@Body() createCompanyDto: CreateCompanyDto) {
    return this.companyService.create(createCompanyDto);
  }

  @Get(":id")
  @Roles("admin")
  @ApiOperation({
    summary: "根据ID获取某一个公司的信息，只有管理员拥有该权限",
  })
  findById(@Param("id") id: string) {
    return this.companyService.find(id);
  }

  @Put(":id")
  @Roles("admin")
  @ApiOperation({
    summary: "根据ID修改公司信息，只有管理员拥有该权限",
  })
  update(@Param("id") id: string, @Body() updateCompanyDto: UpdateCompanyDto) {
    return this.companyService.update(id, updateCompanyDto);
  }

  @Delete(":id")
  @Roles("admin")
  @ApiOperation({
    summary: "删除公司，只有管理员拥有该权限",
  })
  delete(@Param("id") id: string) {
    return this.companyService.remove(id);
  }
}
